//
//  DataSelectView.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/19.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "DataSelectView.h"

@interface DataSelectView()

@property (nonatomic,strong) UIView *dismissView;
@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UIDatePicker *datePicker;

@end

@implementation DataSelectView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]) {
        self.alpha = 0;
        
        _dismissView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 225)];
        _dismissView.userInteractionEnabled = YES;
        [_dismissView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)]];
        _dismissView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3];
        
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 225, SCREEN_WIDTH, 225)];
        _bgView.backgroundColor = [UIColor whiteColor];
        
        UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        UIBarButtonItem *dismissBtn = [[UIBarButtonItem alloc] initWithTitle:@"   取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelEvent)];
        UIBarButtonItem *spaceBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *submitBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定   " style:UIBarButtonItemStylePlain target:self action:@selector(submitEvent)];
        toolBar.items = @[dismissBtn,spaceBtn,submitBtn];
        
        [_bgView addSubview:toolBar];
        [self addSubview:_bgView];
        [self addSubview:_dismissView];
        
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, 181)];
        _datePicker.datePickerMode = UIDatePickerModeDate;
        [_bgView addSubview:_datePicker];

    }
    return self;
}

- (void)submitEvent{
    NSDate *theDate = _datePicker.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"YYYY-MM-dd";
    NSString *str = [dateFormatter stringFromDate:theDate];

    if (self.selectHandle) {
        self.selectHandle(str);
    }
    
    [self dismiss];
}

- (void)cancelEvent{
    [self dismiss];
}

- (void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

- (void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
